package com.wwfish.cmsui.modules.sysuser.client.widget;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.nexustar.gwt.widgets.client.asyn.IAsyncModelCallback;
import com.nexustar.gwt.widgets.client.ui.iprovider.ITreeProvider;
import com.nexustar.gwt.widgets.client.ui.tree.CheckBoxTree;
import com.wwfish.cms.model.ChannelDto;
import com.wwfish.cms.model.sysuser.AccessItemDto;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2010-8-19
 * Time: 10:20:31
 * To change this template use File | Settings | File Templates.
 */
public class AccessChannelTreeWidget extends Composite {
    private CheckBoxTree tree;
    private ITreeProvider provider = new ITreeProvider() {
        public String getTreeItemText(Object entity) {
            if (entity instanceof ChannelDto) {
                ChannelDto dto = (ChannelDto) entity;
                return dto.getName();
            } else {
                AccessItemDto dto = (AccessItemDto) entity;
                return dto.getName();
            }
        }

        public Image getTreeItemIcon(Object entity) {
            return null;
        }

        public boolean hasChildren(Object entity) {

            return entity instanceof ChannelDto;
        }

        public void getChildren(Object parentEntity, IAsyncModelCallback asyn) {
            ChannelDto dto = (ChannelDto) parentEntity;
            if (dto.getChildren() != null && dto.getChildren().size() > 0)
                asyn.setModelElments(dto.getChildren());
            else {
                List accesses = new ArrayList(2);
                AccessItemDto publish = new AccessItemDto();
                publish.setName("发布");
                publish.setCode(dto.getId().toString());
                AccessItemDto audit = new AccessItemDto();
                audit.setName("审核");
                audit.setCode(dto.getId().toString());
                accesses.add(publish);
                accesses.add(audit);
                asyn.setModelElments(accesses);

            }
        }

        public void setInputData(Object[] paramters, IAsyncModelCallback asyn) {

        }
    };


    public AccessChannelTreeWidget() {
        tree = new CheckBoxTree(provider, CheckBoxTree._CASCADE_CHILDREN);

        initWidget(tree);
    }
    public CheckBoxTree getTree(){
        return tree;
    }

}
